<?php
namespace Home\Controller;
use Think\Controller;
class BaseController extends Controller{

    public function getCode(){
        //生成验证码图片
        session_start();
        Header("Content-type: image/PNG");
        srand((double)microtime()*1000000);
        $im = imagecreatetruecolor(62,25);
        $grey = imagecolorallocate($im, 255,255,255);  //背景颜色
        $white = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200)); //文字颜色
        $black = imagecolorallocate($im, 0,0,0);
        imagerectangle($im,0,0,61,24,$white);
        imagefill($im,60,23,$grey);
        $authnum=I('get.num')?I('get.num'):rand(1000,9999);
        $_SESSION['authnum'] = $authnum;
        //将四位整数验证码绘入图片
        imagestring($im,5,15,4, $authnum, $white);
        for($i=0;$i<500;$i++)   //加入干扰象素
        {
            $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
            imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
        }
        imagepng($im);
        imagedestroy($im);
    }
    public function test(){
        session_start();
        if(is_post){
            $num = I('post.num');
            $token = I('post.token');
            if($num == $_SESSION['authnum'] && $token == $_SESSION['token']){
                unset($_SESSION['token']);
                echo 1;
            }
            else{
                echo 0;
            }
        }
    }


}
?>